package erfgame.core.world.terrain;

import java.util.WeakHashMap;

import erfgame.core.world.World;

public class BlockSurfaceImageFactoryManager {
	private WeakHashMap<World,BlockSurfaceImageFactory> factories;
	private TerrainColorSourceFactory unknownTerrainColorSourceFactory;
	private TerrainColorSourceIndexFactory colorSourceIndexFactory;
	
	public BlockSurfaceImageFactoryManager( TerrainColorSourceFactory unknownTerrainColorSourceFactory, TerrainColorSourceIndexFactory colorSourceIndexFactory) {
		this.factories = new WeakHashMap<World, BlockSurfaceImageFactory>(1);
		this.unknownTerrainColorSourceFactory = unknownTerrainColorSourceFactory;		
		this.colorSourceIndexFactory = colorSourceIndexFactory;
	}
	
	public BlockSurfaceImageFactory getFactory( World world ) {
		BlockSurfaceImageFactory result = this.factories.get( world );
		if( result == null ) {
			TerrainColorSource unknownColorSource;
			unknownColorSource = this.unknownTerrainColorSourceFactory.createColorSource(world);
	
			TerrainColorSourceIndex index = colorSourceIndexFactory.createIndex(world);
			
			result = new BlockSurfaceImageFactory(
					world,
					index, 
					unknownColorSource,
					TerrainConstants.Y_SPACING*4
			);
			this.factories.put( world, result );
		}
		return result;
	}
}
